package com.scalar.chiptrack.sales.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.sales.ProductCommission;
import com.scalar.chiptrack.sales.forms.SalesCommissionForm;
import com.scalar.chiptrack.utils.SqlQueryMappings;


/**
 * Implementation of <strong>DAO</strong> business logic for
 * search with sales order
 *
 * @author Raj Vallamsetla
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:19 $
 */


public class SalesCommissionDAO
{

   // ----------------------------------------------------- Instance Variables
   

   /**
    * Default Constructor
    *
    */
   public SalesCommissionDAO()
   {

   }

    public static SalesCommissionForm getSalesCommission () throws ScalarException {

        Connection conn = null;
        CallableStatement cstmt = null;
        ResultSet rset = null;
        SalesCommissionForm salesCommission = new SalesCommissionForm();

        
        try {
            conn = DataSource.getConnection();
            cstmt = conn.prepareCall (SqlQueryMappings.get ("sales.salescommission.getTotalCommission"));
            rset = cstmt.executeQuery();    
            while (rset.next()) {
					salesCommission.setSalesCommission(rset.getString("Total_Commission"));
					salesCommission.setType(rset.getString("Type"));
                  }
        }
        catch (SQLException se) {
            //se.printStackTrace();
            if (se.getMessage().indexOf("No ResultSet") == -1) {
                throw new ScalarException ("Could not retrieve Sales Commission Data from database", se);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new ScalarException ("Could not retrieve Sales Commission Data from database", e);
        }
        finally {
            if (rset != null) {
                try { rset.close(); } catch (SQLException ignore) {}
            }
            if (cstmt != null) {
                try { cstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

        return salesCommission;
    }  
   
		
		public static ArrayList productCommission () throws ScalarException {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        ArrayList productCommissionList = new ArrayList();

        try {
            conn = DataSource.getConnection();

            pstmt = conn.prepareCall (SqlQueryMappings.get ("operations.sales.salescommission.productCommission"));
            rset = pstmt.executeQuery();    

            ProductCommission productCommission = null;
            while (rset.next()) {
                productCommission = new ProductCommission();
                productCommission.setProductId(rset.getString ("Product_ID"));
                productCommission.setMarketingPartNumber (rset.getString ("Label"));
                productCommission.setProductCommission(rset.getString("P_Marketing_Part_Number"));
                productCommissionList.add (productCommission);
            }
        }
        catch (Exception e) {
            throw new ScalarException ("Could not retrieve PackingSlip list from database", e);
        }
        finally {
            if (rset != null) {
                try { rset.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

        return productCommissionList;
    }




        
}